package com.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.proj.InfoInterface;
import com.proj.ViewInterface;

import static com.controller.Utility.errBadRequest;
import static com.controller.Utility.parsePayloadToClassThen;

public class ViewController {
    ViewInterface viewInterface;
    InfoInterface infoInterface;
    SpecialTagController specialTagController = new SpecialTagController();

    public String handle(String payload) {
        String op = (String) JSON.parseObject(payload).get("type");
        String response;

        switch (op) {
            case "req_moveNode": {
                response = parsePayloadToClassThen(payload,
                        JSONObject.class,
                        value -> {
                            String from = value.getObject("from", String.class);
                            String to = value.getObject("to", String.class);
                            return viewInterface.moveNode(from, to);
                        });
                break;
            }
            case "req_addNode": {
                response = parsePayloadToClassThen(payload,
                        JSONObject.class,
                        value -> {
                            String path = value.getObject("path", String.class);
                            JSONObject node = value.getObject("node", JSONObject.class);
                            return viewInterface.addNode(path, node);
                        });
                break;
            }
            case "req_getStructureOfChildTree": {
                response = parsePayloadToClassThen(payload,
                        JSONObject.class,
                        value -> {
                            // TODO: 此处节点中只有info和tag的id，不包含实际的数据
                            // TODO: 还要递归地填充每个节点对象的infos数组，才能返回
                            // TODO: 还要递归地填充每个节点对象的tags数组，才能返回
                            return JSON.toJSONString(new jResultCrate<>(
                                    "resp_getStructureOfChildTree",
                                    viewInterface.getStructureOfChildTree((String) value.get("path"))
                            ));
                        });
                break;
            }
            case "req_addTagToNode": {
                response = parsePayloadToClassThen(payload,
                        jExistingPathTag.class,
                        value -> viewInterface.addTagToNode(value.path, value.tagId));
                break;
            }
            case "req_deleteTagFromNode": {
                response = parsePayloadToClassThen(payload,
                        jExistingPathTag.class,
                        value -> viewInterface.deleteTagFromNode(value.path, value.tagId));
                break;
            }
            case "req_addInfo": {
                response = parsePayloadToClassThen(payload,
                        JSONObject.class,
                        value -> {
                            // 用接口JSONObject getTagUnion(String path)，把获取到的TagUnion添加到info中
                            JSONObject tags = viewInterface.getTagUnion(value.getString("path"));
                            for (String tag : tags.keySet()) {
                                infoInterface.addTagForInfo(tag, value.getString("infoId"));
                            }
                            return "ok";
                        });
                break;
            }
            case "req_deleteInfo": {
                response = parsePayloadToClassThen(payload,
                        JSONObject.class,
                        value -> {
                            // 用接口JSONObject getTagUnion(String path)，把获取到的TagUnion从info中删除
                            JSONObject tags = viewInterface.getTagUnion(value.getString("path"));
                            for (String tag : tags.keySet()) {
                                infoInterface.removeTagForInfo(tag, value.getString("infoId"));
                            }
                            return "ok";
                        });
                break;
            }
            case "req_getInfosByTagUnion": {
                response = specialTagController.handle(payload);
                break;
            }
            default: {
                response = errBadRequest();
                break;
            }
        }

        return response;
    }

}
